﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Controls.Classes;
using System.Drawing;

namespace Silvermoon.Controls
{
    public class ToolTip : ToolTipBase
    {
        #region fields

        private ControlCollection<ImageButton> buttons;
        private Size buttonSize;

        #endregion
        #region ctor

        public ToolTip()
            : base()
        {
            ButtonSize = new Size(72, 72);
            buttons = new ControlCollection<ImageButton>(this);
        }

        public override void Dispose()
        {
            buttons.Clear();
            base.Dispose();
        }

        #endregion
        #region props

        public ControlCollection<ImageButton> Buttons { get { return buttons; } }

        protected internal override IList<Control> SubControls { get { return buttons.Controls; } }

        #endregion
        #region Methods

        public Size ButtonSize { get { return buttonSize; } set { if (buttonSize != value) { buttonSize = value; Invalidate(InvalidationFlag.Size | InvalidationFlag.Layout); } } }

        protected override void PreLayout()
        {
            base.PreLayout();
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Margin | InvalidationFlag.LayoutAppearance))
            {
                int w = buttons.Count * (ButtonSize.Width + 8) + 32 - 8;
                int h = ButtonSize.Height + 40;
                int y = Position == ToolTipPosition.Below ? 32 : 16;

                Bounds = new Rectangle(Left, Top, w, h);
                ClientBounds = new Rectangle(16, y, w - 16, h - 24);
            }
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Margin | InvalidationFlag.LayoutAppearance))
            {
                int n = buttons.Count;
                if (n > 0)
                {
                    int w = ClientWidth;
                    int h = ClientHeight;
                    int cellWidth = w / n;

                    int x = ClientBounds.Left;
                    int y = ClientBounds.Top;
                    int bw = ButtonSize.Width;
                    int bh = ButtonSize.Height;

                    foreach (ImageButton btn in buttons)
                    {
                        btn.Bounds = new Rectangle(x, y, bw, bh);
                        x += bw + 8;
                    }
                }
            }
            base.OnLayout();
        }

        #endregion

    }
}
